<template>
    <div class="list" ref="wrap">
      <div>
        <div class="area">
          <div class="area__tile border-topbottom">当前城市</div>
          <div class="btn-list">
            <div class="btn-wrapper">
              <div class="btn border">{{this.city}}</div>
            </div>
          </div>
        </div>
        <div class="area">
          <div class="area__tile border-topbottom">热门城市</div>
          <div class="btn-list">
            <div class="btn-wrapper"
                 v-for="item of hotCity"
                 :key="item.id"
                 @click="handleCityClick(item.name)"
            >
              <div class="btn border">{{item.name}}</div>
            </div>
          </div>
        </div>
        <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
          <div class="area__tile border-topbottom">{{key}}</div>
          <div class="item-list">
            <div class="item border-bottom"
                 v-for="innerItem of item"
                 :key="innerItem.id"
                 @click="handleCityClick(innerItem.name)"
            >{{innerItem.name}}</div>
          </div>
        </div>
      </div>
    </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'cityList',
  props: {
    cities: Object,
    hotCity: Array,
    letter: String
  },
  methods: {
    handleCityClick (city) {
      this.changeIt(city)
      this.$router.push('/')
    },
    ...mapMutations(['changeIt'])
  },
  computed: {
    ...mapState(['city'])
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrap)
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>

<style lang='stylus' scoped>
  .list
    overflow: hidden;
    position: absolute;
    top: 166px;
    left: 0;
    right: 0;
    bottom: 0;
  .border-topbottom
    &:before
      border-color: #ccc;
    &:after
      border-color: #ccc;
  .area__tile
    padding-left: 20px;
    height: 50px;
    line-height: 50px;
    background: #eee;
    color: #666;
  .btn-list
    overflow: hidden;
    width: 100%;
    padding: 10px 30px 10px 10px;
  .btn-wrapper
    float: left;
    width: 33.33%;
    padding: 10px;
    text-align: center;
    .btn
      width: 100%;
      height: 44px;
      line-height: 44px;
      color: #666;
      &.border::before
          border-color: #b6b6b6
          border-radius: 6px;
  .item-list
    .item
      padding-left: 20px;
      height: 70px;
      line-height: 70px;
      color: #666;
      &.border-bottom:before
        border-color: #ccc;
</style>
